package com.ruoyi.common.utils;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author chengliang
 * @date 2021/11/26
 **/
@Component
public class OrderNoUtil {
    private static final Logger log = LoggerFactory.getLogger(OrderNoUtil.class);

    @Autowired
    private RedisCache cache;

    private final static String DATEFORMAT = "yyMMddHHmmssSSS";
    /**
     * 订单类别头
     */

    private static final String ORDER_CODE = "";
    /**
     * 退款类别头
     */

    private static final String REFUND_ORDER = "";


    /**
     * 随即编码
     */

    private static final int[] r = new int[]{7, 9, 6, 2, 8, 1, 3, 0, 5, 4};
    /**
     * 用户id和随机数总长度
     */

    private static final int maxLength = 5;

    /**
     * 根据id进行加密+加随机数组成固定长度编码
     */
    private static String toCode(Long userId) {
        String idStr = userId.toString();
        StringBuilder idsbs = new StringBuilder();
        for (int i = idStr.length() - 1; i >= 0; i--) {
            idsbs.append(r[idStr.charAt(i) - '0']);
        }
        return idsbs.append(getRandom(maxLength - idStr.length())).toString();
    }

    /**
     * 生成时间戳
     */
    private static String getDateTime() {
        DateFormat sdf = new SimpleDateFormat(DATEFORMAT);
        return sdf.format(new Date());
    }

    /**
     * 生成固定长度随机码
     *
     * @param n 长度
     */

    private static long getRandom(long n) {
        long min = 1, max = 9;
        for (int i = 1; i < n; i++) {
            min *= 10;
            max *= 10;
        }
        long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min;
        return rangeLong;
    }


    /**
     * 生成不带类别标头的编码
     *
     * @param userId
     */
    private static synchronized String getCode(Long userId) {
        userId = userId == null ? 10000 : userId;
        return getDateTime() + toCode(userId);
    }


    public static void testGenerateOrderNo() {
        List<String> orderList = new ArrayList<String>();
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 1; i++) {
            String orderId = OrderNoUtil.getOrderNo(1L);
            System.out.println(orderId);
            System.out.println(orderId.length());
            if (orderList.contains(orderId)) {
                System.out.println(orderId);
                log.info("orderId:{}", orderId);
                log.info("orderList:{},list:{}", new Object[] { orderList.size(), orderList });
                log.error("订单号重复");
            }
            orderList.add(orderId);
        }
        log.info("orderNoUtil cost:{}", System.currentTimeMillis() - startTime);
    }

    public static void main(String[] args) {
        testGenerateOrderNo();
    }

    /**
     * 生成订单单号编码
     */

    public static String getOrderNo(Long userId) {
        return ORDER_CODE + getCode(userId);
    }

    /**
     * 生成退款单号编码
     */
    public static String getRefundCode(Long userId) {
        return REFUND_ORDER + getCode(userId);
    }



    /**
     * @param digit 位数
     * @return 随机生成digit位数的数字
     */
    public static String getNum(int digit) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < digit; i++) {
            if (i == 0 && digit > 1)
                str.append(new Random().nextInt(9) + 1);
            else
                str.append(new Random().nextInt(10));
        }
        return str.toString();
    }



}
